package com.texas_android.app.View;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.texas_android.app.Appconfig.AppConfig;
import com.texas_android.app.Appconfig.AppContext;
import com.texas_android.app.Appconfig.MyApplication;
import com.texas_android.app.R;
import com.texas_android.app.http.Api;
import com.texas_android.app.http.ResponseHandler;
import com.texas_android.app.http.bean.TokenBean;
import com.texas_android.app.utils.AppOperator;
import com.texas_android.app.utils.DialogHelper;
import com.texas_android.app.utils.cathe.ACache;
import com.universal_library.activity.BaseActivity;
import com.universal_library.permission.Acp;
import com.universal_library.permission.AcpListener;
import com.universal_library.permission.AcpOptions;
import com.universal_library.weight.AppToast;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.et_name)
    EditText et_name;

    @Bind(R.id.et_pass)
    EditText et_pass;

    @Bind(R.id.bt_login)
    Button bt_login;

    @Bind(R.id.tv_login_title)
    TextView tv_login_title;
    private Dialog loading = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        AppContext.addActivity(LoginActivity.this);
        requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO);
        SpannableString spannableString = new SpannableString("领导班子和领导干部适配指数系统v1.0");
        RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(0.6f);
        spannableString.setSpan(relativeSizeSpan, 16, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv_login_title.setText(spannableString);
        et_name.setText("18200000000");
        et_pass.setText("111111");
    }

    @Override
    protected void initData() {


    }

    public void requestPermission(String... permission) {
        Acp.getmInstance(this).request(new AcpOptions.Builder().setPermissions(permission).build(), new AcpListener() {
            @Override
            public void onGranted() {
                //获取权限成功
            }

            @Override
            public void onDenied(List<String> permissions) {
                //获取权限失败
            }
        });
    }

    @OnClick({R.id.bt_login})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login:

                login();
                break;
        }
    }

    private ResponseHandler loginHandler = new ResponseHandler(this, false, "登录") {
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
            loading.dismiss();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            super.onSuccess(statusCode, headers, responseString);
            if (baseBean.getStatus().equals("0")) {
                //成功  保存账号密码  保存3天
                TokenBean bean = AppOperator.createGson().fromJson(responseString, new TypeToken<TokenBean>() {
                }.getType());
                MyApplication.aCache.put(AppConfig.APP_LOGIN_PHONE, et_name.getText().toString().trim());
                MyApplication.aCache.put(AppConfig.APP_LOGIN_PASS, et_pass.getText().toString().trim());
                MyApplication.aCache.put(AppConfig.APP_SERVER_TOKEN, bean.getToken(), 3 * ACache.TIME_DAY);
                Intent intent = new Intent(LoginActivity.this, SearchActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.slide_in_top, R.anim.slide_in_top);
                LoginActivity.this.finish();
            }
            loading.dismiss();
        }
    };

    public void login() {
        if (TextUtils.isEmpty(et_name.getText().toString())) {
            AppToast.showToast(getApplicationContext(), "", "请输入用户名");
            return;
        }
        if (TextUtils.isEmpty(et_pass.getText().toString())) {
            AppToast.showToast(getApplicationContext(), "", "请输入密码");
            return;
        }
        loading = DialogHelper.loadingDialog("", this);
        loading.show();
        Api.LoginIn(loginHandler, et_name.getText().toString(), et_pass.getText().toString());
    }
}
